package com.betterjr.modules.order.dubbo;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.betterjr.common.web.AjaxObject;
import com.betterjr.modules.order.IScfInvoiceService;
import com.betterjr.modules.order.entity.ScfInvoice;
import com.betterjr.modules.order.entity.ScfInvoiceDO;
import com.betterjr.modules.order.entity.ScfInvoiceDOItem;
import com.betterjr.modules.order.entity.ScfInvoiceItem;
import com.betterjr.modules.order.service.ScfInvoiceDOService;
import com.betterjr.modules.order.service.ScfInvoiceItemService;
import com.betterjr.modules.order.service.ScfInvoiceService;
import com.betterjr.modules.rule.service.RuleServiceDubboFilterInvoker;

@Service(interfaceClass = IScfInvoiceService.class)
public class ScfInvoiceDubboService implements IScfInvoiceService {

    @Autowired
    private ScfInvoiceService scfInvoiceService;
    @Autowired
    private ScfInvoiceItemService scfInvoiceItemService;
    @Autowired
    private ScfInvoiceDOService invoiceService;

    @Override
    public String webAddInvoice(final Map<String, Object> anMap, final String anInvoiceItemIds,
            final String anFileList) {
        ScfInvoice anInvoice = (ScfInvoice) RuleServiceDubboFilterInvoker.getInputObj();
        // 保存附件信息
        return AjaxObject.newOk("订单发票信息录入成功", scfInvoiceService.addInvoice(anInvoice, anInvoiceItemIds, anFileList))
                .toJson();
    }

    @Override
    public String webQueryInvoiceList(final Map<String, Object> anMap, final String anFlag, final int anPageNum,
            final int anPageSize) {
        Map<String, Object> anQueryConditionMap = (Map<String, Object>) RuleServiceDubboFilterInvoker.getInputObj();

        return AjaxObject.newOkWithPage("订单发票信息查询成功",
                scfInvoiceService.queryInvoice(anQueryConditionMap, anFlag, anPageNum, anPageSize)).toJson();
    }

    @Override
    public String webSaveModifyInvoice(final Map<String, Object> anMap, final String anInvoiceItemIds,
            final String anFileList) {
        ScfInvoice anInvoice = RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject
                .newOk("订单发票信息编辑成功", scfInvoiceService.saveModifyInvoice(anInvoice, anFileList, anInvoiceItemIds))
                .toJson();
    }

    @Override
    public String webAddInvoiceItem(final Map<String, Object> anMap) {
        ScfInvoiceItem anInvoiceItem = RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject.newOk("发票详情录入成功", scfInvoiceItemService.addInvoiceItem(anInvoiceItem)).toJson();
    }

    @Override
    public String webSaveDeleteInvoice(final Long anId) {
        return AjaxObject.newOk("删除发票信息成功", scfInvoiceService.saveDeleteInvoice(anId)).toJson();
    }

    @Override
    public String webQueryIncompletedInvoice(final Map<String, Object> anMap, final String anFlag, final int anPageNum,
            final int anPageSize) {
        Map<String, Object> queryMap = RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject.newOkWithPage("查询补录发票成功",
                scfInvoiceService.queryIncompletedInvoice(queryMap, anFlag, anPageNum, anPageSize)).toJson();
    }

    @Override
    public String webAddInvoiceDO(final Map<String, Object> anAnMap, final String anFileList,
            final boolean anConfirmFlag, final List<ScfInvoiceDOItem> anInvoiceItemList) {

        ScfInvoiceDO invoice = RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject
                .newOk("发票信息添加成功", invoiceService.addInvoice(invoice, anFileList, anConfirmFlag, anInvoiceItemList))
                .toJson();

    }

    @Override
    public String webSaveModifyInvoiceDO(final Map<String, Object> anAnMap, final String anFileList,
            final boolean anConfirmFlag, final List<ScfInvoiceDOItem> anInvoiceItemList) {

        ScfInvoiceDO invoice = RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject
                .newOk("发票信息编辑成功",
                        invoiceService.saveModifyInvoice(invoice, anFileList, anConfirmFlag, anInvoiceItemList))
                .toJson();

    }

    @Override
    public String webSaveAnnulInvoice(final String anRefNo, final String anVersion) {

        return AjaxObject.newOk("发票废止成功", invoiceService.saveAnnulInvoice(anRefNo, anVersion)).toJson();
    }

    @Override
    public String webSaveAuditInvoiceByRefNoVersion(final String anRefNo, final String anVersion) {

        return AjaxObject.newOk("发票审核成功", invoiceService.saveAuditInvoice(anRefNo, anVersion)).toJson();
    }

    @Override
    public String webQueryIneffectiveInvoice(final Map<String, Object> anAnMap, final String anFlag,
            final int anPageNum, final int anPageSize, final boolean anIsAudit) {

        Map<String, Object> conditionMap = (Map<String, Object>) RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject
                .newOkWithPage("发票查询成功",
                        invoiceService.queryIneffectiveInvoice(conditionMap, anFlag, anPageNum, anPageSize, anIsAudit))
                .toJson();

    }

    @Override
    public String webQueryEffectiveInvoice(final Map<String, Object> anAnMap, final String anFlag, final int anPageNum,
            final int anPageSize, final boolean anIsCust) {

        Map<String, Object> conditionMap = (Map<String, Object>) RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject
                .newOkWithPage("发票查询成功",
                        invoiceService.queryEffectiveInvoice(conditionMap, anFlag, anPageNum, anPageSize, anIsCust))
                .toJson();

    }

    @Override
    public String webFindInvoiceDO(final String anRefNo, final String anVersion) {

        return AjaxObject.newOk("发票查询成功", invoiceService.findInvoice(anRefNo, anVersion)).toJson();
    }

    @Override
    public String webQueryRecycleInvoice(final Map<String, Object> anAnMap, final String anFlag, final int anPageNum,
            final int anPageSize) {

        Map<String, Object> conditionMap = (Map<String, Object>) RuleServiceDubboFilterInvoker.getInputObj();
        return AjaxObject.newOkWithPage("发票查询成功",
                invoiceService.queryRecycleInvoice(conditionMap, anFlag, anPageNum, anPageSize)).toJson();

    }

    @Override
    public String webSaveAnnulEffectiveInvoice(final String anRefNo, final String anVersion) {

        return AjaxObject.newOk("发票废止成功", invoiceService.saveCustAnnulInvoice(anRefNo, anVersion)).toJson();
    }

    /** 
     * <p>Overriding Method of webQueryNotUserIneffectiveInvoice</p>  
     * <p>Description: </p> 
     * @Description: 分页查询已登记，生效且未使用发票 
     * @param anPageNum 页码
     * @param anPageSize  页面大小
     * @param anFlag    是否统计总数
     * @param custNo    供应商编号   
     * @return 分页查询已登记，生效且未使用发票列表
     * @see com.betterjr.modules.order.IScfInvoiceService#webQueryNotUserIneffectiveInvoice(int, int, int, java.lang.Long) 
     * @author xuzx
     * @date 2017年11月16日 下午4:32:28 
     */
    @Override
    public String webQueryNotUserIneffectiveInvoice(final int anPageNum, final int anPageSize, final int anFlag,
            final Long anCustNo) {
        // TODO Auto-generated method stub
        return AjaxObject
                .newOkWithPage("分页查询已登记，生效且未使用发票成功",
                        invoiceService.queryNotUserIneffectiveInvoice(anPageNum, anPageSize, anFlag, anCustNo))
                .toJson();
    }

}
